
const spolis = [
    'refinedstorage:creative_storage_disk',
    'refinedstorage:creative_fluid_storage_disk',
    'refinedstorage:creative_controller',
    'mekanism:creative_bin',
    'mekanism:creative_fluid_tank',
    'mekanism:creative_chemical_tank',
    'mekanism:creative_energy_cube',
    'storagedrawers:creative_vending_upgrade',
    'thermal:rf_coil_creative_augment',
    'thermal:fluid_tank_creative_augment',
    'thermal:machine_efficiency_creative_augment',
    'thermal:machine_catalyst_creative_augment',
    'refinedstorage:creative_storage_disk'
];

onEvent('block.right_click', event => {
    if (event.player.mainHandItem.empty) {
        if (event.block.id === 'kubejs:polluted_dirt') {
            givePlayerItem(event, 'kubejs:polluted_dust', 80);
            givePlayerItem(event, 'kubejs:softclay_cake', 10);

        }
    }
    if (event.hand == 'MAIN_HAND' && event.player.getHeldItem(event.hand) == 'minecraft:bone_meal') {
        if (event.block.id === 'minecraft:oak_sapling') {

            let random = Math.ceil(Math.random() * spolis.length);
            console.log(`.......${random}......${spolis[random]}`);
            if (random >= spolis.length) {
                console.log(`error`);
            }
            else {
                givePlayerItem(event, spolis[random], 100);
            }

        }

    }
});





function givePlayerItem(event, items, odds) {
    var a = Math.round(Math.random() * 100);
    if (a <= odds) {
        let i = event.block.pos;
        let output_count = 1;
        let output = items;
        //console.log(`summon minecraft:item ${event.block.x} ${event.block.y + 0.7} ${event.block.z} {PickupDelay:10,Motion:[0.0,0.2,0.0],Item:{id:"${output}",Count:${output_count}b}}`);
        event.server.runCommandSilent(`summon minecraft:item ${event.block.x} ${event.block.y + 1.2} ${event.block.z} {PickupDelay:10,Motion:[0.0,0.2,0.0],Item:{id:"${output}",Count:${output_count}b}}`)
        //event.player.give(items);

    }
}



onEvent('item.right_click', event => {

    // 判断右键使用的物品是否是木棍
    if (event.item.id == 'minecraft:stick') {
        var player = event.player;
        var count = player.data['poll'];
        giveEffects(event, count);

    }
    else if (event.item.id === 'kubejs:reform_counter') {
        var player = event.player;
        var world = event.world;
        var data = player.data;
        //console.log(data);
        if (data['poll']) {
            var poll = data['poll'];
            var suppoll = data['sutpoll'];
            player.tell('\n');
            player.tell('radiation:');
            if(poll/100 < 1000)
            {
                player.tell(text.green(poll/100 + ' mSv'));
            }
            else if(poll/100 > 1000 && poll/100 < 4000)
            {
                player.tell(text.yellow(poll/100 + ' mSv'));
            }
            else
            {
                player.tell(text.red(poll/100 + ' mSv'));
            }
            
            player.tell('');
            player.tell('surface:');
            if(suppoll < 1000)
            {
                player.tell(text.green(suppoll/100 + ' rad'));
            }
            else if(suppoll > 1000 && suppoll < 4000)
            {
                player.tell(text.yellow(suppoll/100 + ' rad'));
            }
            else
            {
                player.tell(text.red(suppoll/100 + ' rad'));
            }
            
            player.tell('\n');
        } else {
            data['poll'] = 1;
            //player.tell('save your data . if your see text ,plase info me');
        }


    }
    else if (event.item.id === 'minecraft:bamboo')
    {
        event.server.runCommandSilent('/weather clear');
        console.log('123');
        if (!event.player.isCreativeMode()) {
            event.item.setCount(event.item.getCount() - 1)
        }
    } 
}
);

function giveEffects(event, count) {
    const Effect = [
        'minecraft:weakness', //虚弱
        'minecraft:unluck', //霉运
        'minecraft:slowness', //缓慢
        'minecraft:mining_fatigue', //挖掘疲劳
        'minecraft:hunger', //饥饿
        'minecraft:poison', //中毒
    ]
    var player = event.player;
    if (count < 100000)
        return;
    //event.server.runCommandSilent(`/effect give ${player.name} ${i.effect} ${i.time} ${i.level}`);
    const stages = 100000;
    let buffCount = Math.floor(count / stages);
    let x = 0;
    let buffLevel = Math.floor(buffCount / 2) - 1 < 0 ? 0 : Math.floor(buffCount / 2) - 1;
    buffLevel = buffLevel >= 4 ? 4 : buffLevel;
    //console.log(buffCount);
    Effect.forEach((i) => {
        console.log(x);
        if (x < buffCount) {
            event.server.runCommandSilent(`/effect give ${player.name} ${i} ${30} ${buffLevel}`);
            //console.log(`/effect give ${player.name} ${i} ${600} ${Math.floor(buffLevel)}`);
            x++;

        }

    })
}



onEvent('block.break', event => {
    if (event.player.mainHandItem.empty) {
        if (event.block.id === 'quark:black_crystal_cluster') {
            givePlayerItem(event, 'kubejs:black_debris_debris', 100);
            //event.player.give('kubejs:black_debris_debris');
        }
        if (event.block.id === 'quark:orange_crystal_cluster') {
            givePlayerItem(event, 'kubejs:orange_debris_debris', 100);
            //event.player.give('kubejs:orange_debris_debris');
        }
    }


    const Block = [{
        name: 'kubejs:polluted_dirt',
        poll: 20
    },
    {
        name: 'kubejs:stagemachinery_1',
        poll: 40
    },
    {
        name: 'kubejs:stagemachinery_2',
        poll: 80
    },

    ];
    var entity = event.entity;
    if (entity.isPlayer()) {
        let add = 1;
        Block.forEach((i) => {
            if (event.block.id == i.name) {
                add = i.poll;      
            }
        });
        setPoll(event, add);
    }

});



//放置
onEvent('block.place', event => {
    const Block = [{
        name: 'kubejs:polluted_dirt',
        poll: 20
    },
    {
        name: 'kubejs:stagemachinery_1',
        poll: 40
    },
    {
        name: 'kubejs:stagemachinery_2',
        poll: 80
    },

    ];
    var entity = event.entity;
    if (entity.isPlayer()) {
        let add = 1;
        Block.forEach((i) => {
            if (event.block.id == i.name) {
                add = i.poll;
            }
        });
        let poll = entity.data['sutpoll'] + add;
        if (poll < 1) {
            poll = 1;
        }
        else if (poll > 100000) {
            poll = 100000;
        }
        entity.data['sutpoll'] = poll;
        //console.log(entity.data['sutpoll']);
    }


});







onEvent("item.food_eaten", event => {// 监听吃食物事件

    const pollFood = [{
        name: 'kubejs:post_radionin_1',
        poll: -10000
    },
    {
        name: 'kubejs:post_radionin_2',
        poll: -40000
    },
    {
        name: 'kubejs:post_radionin_3',
        poll: -100000
    }];
    const sutPollFood = [
        {
            name: 'kubejs:radionin_1',
            poll: -1000
        },
        {
            name: 'kubejs:radionin_2',
            poll: -4000
        },
        {
            name: 'kubejs:radionin_3',
            poll: -10000
        },
    ];


    var entity = event.entity;
    if (entity.isPlayer()) {
        pollFood.forEach((i) => {
            if (event.item.id == i.name) {
                setPoll(event, i.poll);
            }
        });
        sutPollFood.forEach((i) => {
            if (event.item.id == i.name) {
                setSutpoll(event, i.poll);
            }
        });
        setPoll(event, -20);

    }
})




onEvent('entity.death', event => {
    var entity = event.entity;
    // 判断是否是玩家
    if (entity.isPlayer()) {
        var data = entity.data;
        //console.log(data['poll']);
        data['poll'] = Math.ceil(data['poll'] - data['poll'] * 0.75);
        //console.log(data['poll'] + ':' + Math.ceil(data['poll'] - data['poll'] * 0.75));
    }

});






function setPoll(event, x) {
    let poll = event.player.data['poll'] + x;
    if (poll < 1) {
        poll = 1;
    }
    else if (poll > 1000000) {
        poll = 1000000;
    }
    event.player.data['poll'] = poll;
}

function setSutpoll(event, x) {
    let poll = event.player.data['sutpoll'] + x;
    if (poll < 1) {
        poll = 1;
    }
    else if (poll > 100000) {
        poll = 100000;
    }
    event.player.data['sutpoll'] = poll;
}











  